package io.renren.modules.seller.controller;

import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.entity.SellerEntity;
import io.renren.modules.seller.service.SellerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商家管理
 * @author Mark
 * @date 2020-02-16 11:30:48
 */
@Slf4j
@RestController
@RequestMapping("manage/seller")
public class SellerController {
    @Autowired
    private SellerService sellerService;

    /**
     * 返回全部列表
     *
     * @return list
     */
    @RequestMapping("/findAll")
    public List<SellerEntity> findAll() {
        return sellerService.list();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(String page, String rows) {
        return sellerService.findPage(page, rows);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody SellerEntity seller, String page, String rows) {
        return sellerService.findPage(seller, page, rows);
    }

    /**
     * 增加
     *
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SellerEntity seller) {
        try {
            //状态
            seller.setStatus("0");
            //申请日期
            seller.setCreateTime(new Date());
            sellerService.save(seller);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody SellerEntity seller) {
        try {
            sellerService.updateById(seller);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @return SellerEntity
     */
    @RequestMapping("/findOne")
    public SellerEntity findOne(String id) {
        return sellerService.getById(id);
    }

    /**
     * 批量删除
     *
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(String[] ids) {
        try {
            sellerService.removeByIds(Arrays.asList(ids));
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(String sellerId, String status) {
        try {
            sellerService.updateStatus(sellerId, status);
            return new Result(true, "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("失败");
        }
    }
}
